Web2py Manual
Total Page:16
File Type:pdf, Size:1020Kb
MASSIMODIPIERRO GIOVANNICANNATA WEB2PY MANUAL 3 R D E D I T I O N LULU.COM Copyright 2008, 2009, 2010 by Massimo Di Pierro. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copy- right Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923,(978) 750-8400, fax (978) 646-8600, or on the web at www.copyright.com. Requests to the Copyright owner for permission should be addressed to: Massimo Di Pierro School of Computing DePaul University 243 S Wabash Ave Chicago, IL 60604 (USA) Email: [email protected] Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created ore extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. Library of Congress Cataloging-in-Publication Data: ISBN-0000 Printed in the United States of America. 3 To my family Contents 1 Introduzione 27 1.1 Principi . 30 1.2 Framework di sviluppo web . 31 1.3 Model-View-Controller . 33 1.4 Perchè web2py .......................... 37 1.5 Sicurezza . 38 1.6 Cosa c’è "nella scatola" . 41 1.7 Licenza . 43 1.8 Riconoscimenti . 45 1.9 Struttura del libro . 47 1.10 Elementi di stile . 49 6 2 Il linguaggio Python 51 2.1 Python . 51 2.2 Avvio . 52 2.3 help, dir . 53 2.4 Tipi ................................. 54 2.4.1 str.............................. 55 2.4.2 liste . 56 2.4.3 tuple . 57 2.4.4 dict . 59 2.5 Indentazione del codice . 60 2.6 for... in . 61 2.7 while . 62 2.8 def... return . 63 2.9 if... elif... else . 64 2.10 try... except... else... finally . 65 2.11 classi . 67 2.12 Attributi, metodi ed operatori speciali . 68 2.13 File Input/Output . 69 7 2.14 lambda . 70 2.15 exec, eval . 72 2.16 import . 73 2.16.1 os.............................. 74 2.16.2 sys ............................. 75 2.16.3 datetime . 76 2.16.4 time . 76 2.16.5 cPickle . 77 3 Panormaica di web2py 79 3.1 Avvio . 79 3.2 Hello! . 84 3.3 Contiamo! . 90 3.4 Come ti chiami? . 91 3.5 Auto-invio delle form . 93 3.6 Un Blog con immagini . 97 3.7 Aggiungere le funzionalità CRUD (Create, Read, Update, Delete)112 3.8 Aggiungere l’autenticazione . 114 8 3.9 Wiki................................. 116 3.10 Altre funzionalità di admin ................... 128 3.10.1 site ............................. 128 3.10.2 about ............................ 132 3.10.3 edit ............................. 132 3.10.4 errors ............................ 135 3.10.5 Mercurial .......................... 139 3.11 Altre funzionalità di appadmin ................. 139 4 Il nucleo di web2py 141 4.1 Opzioni della linea di comando . 141 4.2 Indirizzamento delle URL (dispatching) . 144 4.3 Librerie . 149 4.4 Applicazioni . 154 4.5 API ................................. 156 4.6 request . 157 4.7 response . 161 4.8 session . 164 9 4.9 cache . 165 4.10 URL................................. 168 4.11 HTTP e la redirezione . 170 4.12 T e l’internazionalizzazione . 172 4.13 Cookie . 175 4.14 L’applicazione init ........................ 176 4.15 Riscrittura delle URL . 176 4.16 Instradamenti degli errori . 180 4.17 Cron ................................ 181 4.18 Processi in backgroup e code dei task . 184 4.19 Moduli di terze parti . 186 4.20 L’ambiente d’esecuzione . 187 4.21 Cooperazione . 189 4.22 WSGI . 190 4.22.1 Middleware esterno . 191 4.22.2 Middleware interno . 192 4.22.3 Chiamare le applicazioni WSGI . 192 4.23 Sintassi di base . 196 10 4.23.1 for... in . 196 4.23.2 while . 196 4.23.3 if... elif... else . 197 4.23.4 try... except... else... finally . 198 4.23.5 def... return . 199 4.24 Helper HTML . 200 4.24.1 XML ............................ 201 4.24.2 Gli helper di web2py................... 202 4.24.3 Helper personalizzati . 214 4.25 BEAUTIFY . 216 4.26 DOM server side e Parsing . 217 4.26.1 elements . 217 4.26.2 parent . 219 4.26.3 flatten . 219 4.26.4 Parsing . 220 4.27 Page Layout . 220 4.27.1 Layout di default delle pagine . 222 4.27.2 Personalizzare il layout di default . 224 11 4.28 Funzioni nelle viste . 226 4.29 Blocchi nelle viste . 227 4.30 Utilizzare i template per inviare email . 228 5 Lo strato di astrazione del database 231 5.1 Dipendenze . 231 5.2 Stringhe di connessione . 233 5.2.1 Raggruppamento delle connessioni (Pooling)..... 234 5.2.2 Errori di connessione . 235 5.2.3 Database replicati . 235 5.3 Parole chiave riservate . 236 5.4 DAL, tabelle e campi . 237 5.5 Rappresentazione dei record . 238 5.6 Migrazioni . 244 5.7 Correggere le migrazioni errate . 245 5.8 insert . 246 5.9 Completamento (commit) e annullamento (rollback) delle transazioni . 247 5.10 Codice SQL esplicito . 248 12 5.10.1 executesql . 248 5.10.2 _lastsql . 249 5.11 drop................................. 249 5.12 Indici . 250 5.13 Database pre-esistenti e tabelle con chiave . 250 5.14 Transazioni distribuite . 251 5.15 Upload manuali . 252 5.16 Query, Set, Rows . 253 5.17 select . 254 5.17.1 Abbreviazioni . 256 5.17.2 Recuperare un record . 257 5.17.3 Select ricorsive . 257 5.17.4 Serializzare gli oggetti row nelle viste . 258 5.17.5 orderby, groupby, limitby, distinct . 259 5.17.6 Operatori logici . 261 5.17.7 count, delete, update . 262 5.17.8 Espressioni . 262 5.17.9 update_record . 263 13 5.17.10 first e last . 263 5.17.11 as_dict e as_list . 264 5.17.12 find, exclude, sort . 264 5.18 Campi calcolati . 265 5.19 Campi virtuali . ..